#%%
import ReUseDetection

#%%

bin_paths = [
    "dataset/zlib1.2.8_multi_optimize/libz.so.1.2.8.O0",
    "dataset/zlib1.2.8_multi_optimize/libz.so.1.2.8.O1",
    "dataset/zlib1.2.8_multi_optimize/libz.so.1.2.8.O2",
    "dataset/zlib1.2.8_multi_optimize/libz.so.1.2.8.O3",
    "dataset/zlib1.2.8_multi_optimize/libz.so.1.2.8.Os",

]

my_cfgs = [ReUseDetection.MyCFG(p) for p in bin_paths]

#%%
norm_scores = []
dismatch_ratios = []

for i in range(0, len(my_cfgs)):
    for j in range(i + 1, len(my_cfgs)):
        _norm_scores = ReUseDetection.compareCFG(my_cfgs[i], my_cfgs[j], NUM_THREADS=2)
        norm_scores.append(_norm_scores)
        _ratio, _match = ReUseDetection.my_print(_norm_scores, my_cfgs[i], my_cfgs[j])
        dismatch_ratios.append(_ratio)


#%%

norm_scores2 = []
dismatch_ratios2 = []
for c in my_cfgs:
    _n = ReUseDetection.compareCFG(c, c, 2)
    norm_scores2.append(_n)
    _ratio, _match = ReUseDetection.my_print(_n, c, c)
    dismatch_ratios2.append(_ratio)

#%%

my_cfg1 = my_cfgs[0]

my_cfg2 = my_cfgs[1]

#%%
norm_scores = ReUseDetection.compareCFG(my_cfg1, my_cfg2)

# %%

ReUseDetection.my_print(norm_scores, my_cfg1, my_cfg2)

#%%
func_name = "compress_block"
my_func_node = my_cfg1.get_by_name(func_name)
my_function_nodes = ReUseDetection.filt(my_func_node, my_cfg2.my_functions)

print("过滤比:", 1 - len(my_function_nodes) / len(my_cfg2.my_functions))

scores = ReUseDetection.compare(my_func_node, my_function_nodes)
top_scores = ReUseDetection.top10scores(scores)
top_scores

#%%
ReUseDetection.print_func_node(my_func_node.func_node)

#%%

func_name = "deflateReset"
my_func_node = my_cfg1.get_by_name(func_name)
my_func_node2 = my_cfg2.get_by_name(func_name)

ReUseDetection.compare(my_func_node, [my_func_node2])


#%%

_func_name = top_scores[0][0]
_my_func_node = my_cfg2.get_by_name(_func_name)
ReUseDetection.print_func_node(_my_func_node.func_node)


#%%
ReUseDetection.print_func_node(my_cfg1.get_by_name(func_name).func_node)


#%%
ReUseDetection.print_func_node(my_cfg2.get_by_name(func_name).func_node)

# %%

ReUseDetection.compare(my_cfg1.get_by_name(func_name), [my_cfg2.get_by_name(func_name)])

# %%

ReUseDetection.filt(my_cfg1.get_by_name(func_name), [my_cfg2.get_by_name(func_name)])


# %%
my_cfg1.get_by_name(func_name).all_self_score

# %%

my_cfg2.get_by_name(func_name).all_self_score
